package top.lyjwn.todo.resources.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lyjwn.todo.common.template.R;
import top.lyjwn.todo.resources.config.ResourcesConfig;
import top.lyjwn.todo.resources.data.FileResource;
import top.lyjwn.todo.resources.data.SaveResult;
import top.lyjwn.todo.resources.service.FileResourceRepositoryService;
import top.lyjwn.todo.resources.service.FileResourceService;
import top.lyjwn.todo.resources.util.FileUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文件资源实现
 * 
 * @author luoyujie
 *
 */
@Service
@CacheConfig(cacheNames = "entity:todo-resources:file_resource")
public class FileResourceServiceImpl implements FileResourceService {

	@Autowired(required = false)
	private FileResourceRepositoryService fileResourceRepositoryService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * top.lyjwn.todo.resources.service.FileResourceService#save(java.lang.String,
	 * byte[])
	 */
	@Override
	public R save(String referUsers, Boolean isPublic, String fileName, byte[] fileResource) {
		FileResource fileResource2 = new FileResource();
		fileResource2.setPublic(isPublic);
		fileResource2.setReferUsers(referUsers);
		fileResource2.setCreateTime(new Date());
		fileResource2.setUpdateTime(new Date());
		fileResource2.setFileName(fileName);
		fileResource2.setFileResource(fileResource);

		if (ResourcesConfig.is_file_store){
			if (FileUtil.write(fileName,fileResource)){
				return R.init().ok("保存成功");
			}else{
				return R.init().error(600,"保存失败");
			}
		}else{
			if (fileResourceRepositoryService==null){
				return R.init().error(600,"未实现具体的文件处理方法");
			}
		}
		return fileResourceRepositoryService.save(fileResource2);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * top.lyjwn.todo.resources.service.FileResourceService#save(java.lang.String,
	 * java.lang.Boolean, java.lang.String, java.lang.String[])
	 */
	@Override
	public R save(String referUsers, Boolean isPublic, List<FileResource> fileResources) {
		R<SaveResult> template = R.init().ok("已处理");
		List<SaveResult> saveResults = new ArrayList<>();
		for (FileResource fileResource : fileResources) {
			FileResource fileResource2 = new FileResource();
			fileResource2.setPublic(isPublic);
			fileResource2.setReferUsers(referUsers);
			fileResource2.setCreateTime(new Date());
			fileResource2.setUpdateTime(new Date());
			fileResource2.setFileName(fileResource.getFileName());
			fileResource2.setFileResource(fileResource.getFileResource());
			fileResourceRepositoryService.save(fileResource2);
			SaveResult saveResult = new SaveResult();
			if (ResourcesConfig.is_file_store){
				if (FileUtil.write(fileResource.getFileName(),fileResource.getFileResource())){
					saveResult.setDone(true);
					saveResult.setMsg("保存成功");
				}else{
					saveResult.setDone(false);
					saveResult.setMsg("保存失败");
				}
			}else{
				if (fileResourceRepositoryService==null){
					saveResult.setDone(false);
					saveResult.setMsg("未实现具体的文件处理方法");
					return R.init().error(600,"未实现具体的文件处理方法");
				}
			}
			saveResults.add(saveResult);
		}
		template.setList(saveResults);
		return template;
	}



	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * top.lyjwn.todo.resources.service.FileResourceService#update(top.lyjwn.todo.
	 * data.resources.FileResource)
	 */
	@Transactional
	@CacheEvict(key = "#p0")
	@Override
	public R update(FileResource fileResource) {
		if (fileResourceRepositoryService==null){
			return R.init().error(600,"未实现具体的文件处理方法");
		}
		R<FileResource> byName = fileResourceRepositoryService.findByName(fileResource.getFileName());
		if (byName.isDone()){
			R r = fileResourceRepositoryService.save(fileResource);
			r.setMsg(r.isDone()?"更新成功": r.getMsg());
			return r;
		}else{
			return byName;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * top.lyjwn.todo.resources.service.FileResourceService#delete(java.lang.String)
	 */
	@CacheEvict(key = "#p0")
	@Transactional
	@Override
	public R delete(String name) {
		if (ResourcesConfig.is_file_store){
			if (FileUtil.delete(name)){
				return R.init().ok("成功删除");
			}else{
				return R.init().error(600,"保存失败");
			}
		}else{
			if (fileResourceRepositoryService==null){
				return R.init().error(600,"未实现具体的文件处理方法");
			}
			return fileResourceRepositoryService.deleteByName(name);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * top.lyjwn.todo.resources.service.FileResourceService#findByName(java.lang.String)
	 */
	@Override
	public R<FileResource> findByName(FileUtil.OnFileOut onFileOut, String name) {
		R<FileResource> template;
		if (fileResourceRepositoryService==null){
			template = R.init().error(600,"未实现具体的文件处理方法");
		}else{
			template = fileResourceRepositoryService.findByName(name);
		}
		if (ResourcesConfig.is_file_store){
			if (FileUtil.read(onFileOut,name)){
				return  R.init().ok("找到了资源");
			}else{
				return  R.init().error(200,"查找资源失败了");

			}
		}
		return template;
	}

}
